package app.goods.info.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hpsf.Array;

import app.goods.info.constants.ConstontValue;
import app.goods.info.services.domain.ShopInfo;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class DealShopInfo {
	public static void main(String[] args) {
		// generateAllShops();
		generateAllShopLocation();
	}

	private static void generateAllShopLocation1() {
		String shops = FileReaderUtils.readFileQuikly("configs/target_all_shops.json");
		List<ShopInfo> targetInfos = new ArrayList<ShopInfo>();
		String jsonString = FileReaderUtils.readFileQuikly("configs/all_shops1.json");
		targetInfos.addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), ShopInfo.class));
		targetInfos.forEach(shopInfo -> {
			String address = shopInfo.getShopAddress();
			String url = "https://restapi.amap.com/v3/place/text";
			Map<String, String> queryMap = new HashMap<String, String>();
			queryMap.put("keywords", address);
			queryMap.put("output", "json");
			queryMap.put("city", "宿迁市");
			queryMap.put("key", "4b1109e7a4dff5ac4e90566536e11a36");
			queryMap.put("types", "公司");
			queryMap.put("extensions", "base");

			String res = HttpClientUtils.get1(url, queryMap);
			System.out.println(res);
			try {
				JSONObject result = JSONUtil.parseObj(res);
				if (result != null && result.getJSONArray("pois") != null) {
					JSONObject location = (JSONObject) result.getJSONArray("pois").get(0);
					shopInfo.setLatitude(Double.parseDouble(location.getStr("location").split(",")[1]));
					shopInfo.setLongitude(Double.parseDouble(location.getStr("location").split(",")[0]));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		});
		FileReaderUtils.writeJsonToFileByLine(JSONUtil.toJsonPrettyStr(targetInfos), "configs/all_shops3.json");
	}

	private static void generateAllShopLocation() {
		String shops = FileReaderUtils.readFileQuikly("configs/target_all_shops.json");
		List<ShopInfo> targetInfos = new ArrayList<ShopInfo>();
		String jsonString = FileReaderUtils.readFileQuikly("configs/all_shops1.json");
		targetInfos.addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), ShopInfo.class));
		targetInfos.forEach(shopInfo -> {
			String address = shopInfo.getShopAddress();
			String url = "https://restapi.amap.com/v3/geocode/geo";
			Map<String, String> queryMap = new HashMap<String, String>();
			queryMap.put("address", address);
			queryMap.put("output", "json");
			queryMap.put("city", "宿迁市");
			queryMap.put("key", "4b1109e7a4dff5ac4e90566536e11a36");

			String res = HttpClientUtils.get1(url, queryMap);
			System.out.println(res);
			try {
				JSONObject result = JSONUtil.parseObj(res);
				if (result != null && result.getJSONArray("geocodes") != null) {
					JSONObject location = (JSONObject) result.getJSONArray("geocodes").get(0);
					shopInfo.setLatitude(Double.parseDouble(location.getStr("location").split(",")[1]));
					shopInfo.setLongitude(Double.parseDouble(location.getStr("location").split(",")[0]));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		});
		FileReaderUtils.writeJsonToFileByLine(JSONUtil.toJsonPrettyStr(targetInfos), "configs/all_shops2.json");
	}

	private static void generateAllShops() {
		String shops = FileReaderUtils.readFileQuikly("configs/target_all_shops.json");
		JSONArray allShopsArray = JSONUtil.parseArray(shops);
		List<ShopInfo> targetInfosFinal = new ArrayList<ShopInfo>();
		List<ShopInfo> targetInfos = new ArrayList<ShopInfo>();
		String jsonString = FileReaderUtils.readFileQuikly(ConstontValue.ALL_SHOP_INFO);
		targetInfos.addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), ShopInfo.class));
		allShopsArray.forEach(array -> {
			JSONObject tareJsonObject = (JSONObject) array;
			ShopInfo shopInfo = new ShopInfo();
			shopInfo.setLatitude(0);
			shopInfo.setLongitude(0);
			shopInfo.setShopAddress(tareJsonObject.getStr("shop_address"));
			shopInfo.setShopName(tareJsonObject.getStr("shop_name"));
			shopInfo.setShopNo(tareJsonObject.getStr("shop_no"));
			shopInfo.setStatus(ConstontValue.OFF);

			if (targetInfos.contains(shopInfo)) {
				shopInfo.setStatus(targetInfos.get(targetInfos.indexOf(shopInfo)).getStatus());
			}
			targetInfosFinal.add(shopInfo);
		});
		FileReaderUtils.writeJsonToFileByLine(JSONUtil.toJsonPrettyStr(targetInfosFinal), "configs/all_shops1.json");
	}

}
